// Objects -> key value pairs

const user = {
  name: "John",
  age: 30,
  email: "xmueduajo@emial.com",
  print() {
    // 谁调用orint，this就是谁
    console.log(this.name);
  },
  "user-me": "haha",
};

console.log(user.print());
// ?两种访问方式，这种是对于key是字符串，一般来说，但是普通的name也可以user[name]访问
console.log(user["user-me"]);

//

type User = {
  name: string;
  age: number;
  phone?: string | undefined;
  email?: string;
};

const u1: User = {
  name: "勿哈哈",
  age: 13,
  email: "wixma@emial.com",
  phone: "123456",
};

if (u1.age < 18) {
  // delete的属性只能是可选的。不能删除必选的
  delete u1.phone;
}

console.log(u1);

// Person 挑战

type Person = {
  name: string;
  age: number;
  email: string;
};

const p1: Person = {
  name: "Jonha",
  age: 25,
  email: "john@gmial,com",
};

function updatePersonAge(person: Person, newAge: number): Person {
  // 不要直接修改原对象的属性，最好是用克隆
  person.age = newAge;
  console.log(person);
  return person;
}

updatePersonAge(p1, 18);
console.log(p1);

type Product = {
  name: string;
  price: number;
  quantity: number;
};

const product: Product = {
  name: "ProductA",
  price: 2,
  quantity: 6,
};

function validateProduct(product: Product): boolean {
  return (
    product.name !== "" &&
    product.price > 0 &&
    product.quantity > 0 &&
    Number.isInteger(product.quantity)
  );
}

console.log(validateProduct(product));
